import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:xiaozhou_ams_flutter/todoListInterface.dart';

import 'Face/FaceRecognitionScreen.dart';
import 'UserProfilePage.dart';
import 'memo_list.dart';

class MainPage extends StatefulWidget {
  final int initialIndex;
  const MainPage({Key? key, this.initialIndex = 0}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  late int _selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    // 使用延迟以确保context被正确初始化
    Future.delayed(Duration.zero, () {
      if (!mounted) return; // 在这里检查mounted属性
      final args = ModalRoute.of(context)?.settings.arguments as int?;
      if (args != null) {
        setState(() {
          _selectedIndex = args; // 更新索引
        });
      }
      print("Delayed Selected index: $_selectedIndex"); // 此处打印更新后的索引
    });
  }

  final List<Widget> _widgetOptions = [
    const MemoListPage(),
    const ToDoListScreen(), // 待办事项页面
    const FaceSignInScreen(), // 人脸签到页面
    const UserProfilePage(), // 用户个人主页
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      if (kDebugMode) {
        print("当前选择页：$index");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, // 确保类型为 fixed
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.note,), label: '记事本'),
          BottomNavigationBarItem(icon: Icon(Icons.list), label: '待办事项'),
          BottomNavigationBarItem(icon: Icon(Icons.face), label: '人脸签到'),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: '个人主页'),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.indigo,
        unselectedItemColor: Colors.grey,
        onTap: _onItemTapped,
        backgroundColor: Colors.white,
        elevation: 8,
        selectedLabelStyle: const TextStyle(fontWeight: FontWeight.bold),
        unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.normal),
        showSelectedLabels: true, // 显示选中项的标签
        showUnselectedLabels: true, // 显示未选中项的标签
        selectedFontSize: 12.0, // 选中项的标签字体大小
        unselectedFontSize: 12.0, // 未选中项的标签字体大小
        iconSize: 24.0, // 图标大小
        selectedIconTheme: const IconThemeData(size: 28.0), // 选中项的图标主题
        unselectedIconTheme: const IconThemeData(size: 24.0), // 未选中项的图标主题
      ),
    );
  }
}
